﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using EzDesk.DesktopClient.Command;
using EzDesk.DesktopClient.Model;
using EzDesk.DesktopClient.UtilityView.ViewModel;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.MVVMEnhance;
using EzDesk.EventAggregation;
using EzDesk.Generic.Collections;
using EzDesk.Generic;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.ViewModel
{
    public class TodoViewModel : ViewModelWithSingleContainer<ITodoView>
    {
        public TodoViewModel()
        {
            InitMember();
            InitEvent();
        }

        public RangeCollection<IFeedItemViewModel> TodoItems { get; set; }

        public ICommand ToggleTodoViewExpansionCommand { get; private set; }

        private bool _isExpanded;
        
        public bool IsExpanded
        {
            get { return _isExpanded; }
            private set
            {
                _isExpanded = value;
                RaisePropertyChanged("IsExpanded");
            }
        }

        private void InitMember()
        {
            TodoItems = new RangeCollection<IFeedItemViewModel>();
            IsExpanded = false;
            ToggleTodoViewExpansionCommand = new DelegateCommand<string>(
                p => ToggleTodoViewExpansionEvent.Instance.Publish(!IsExpanded));
        }

        private void InitEvent()
        {
            InitGlobalEvent();
            InitLocalEvent();
        }

        private void InitGlobalEvent()
        {
            FeedPushEvent.Instance.Subscribe(OnFeedUpdate, ThreadOption.UIThread);
        }

        private void InitLocalEvent()
        {
            ToggleTodoViewExpansionEvent.Instance.Subscribe(isExpanded => IsExpanded = isExpanded, ThreadOption.UIThread);
        }

        private void OnFeedUpdate(FeedPushArgs args)
        {
            ResetFeedCollectionRange();
            RefreshTodoItems(args.UpdateCollection);
        }

        private void ResetFeedCollectionRange()
        {
            TodoItems.ResetMaxCount(ClientConfigurationManagement.Instance.FeedDisplayCount);
        }

        private void RefreshTodoItems(IEnumerable<Feed> updateCollection)
        {
            updateCollection.Reverse().Foreach(ProcessPushFeed, this.TodoItems);
        }

        private void ProcessPushFeed(Feed feed, RangeCollection<IFeedItemViewModel> viewModelCollection)
        {
            var itemViewModel = viewModelCollection.FirstOrDefault(uvm => uvm.FeedModel.FeedID == feed.FeedID);
            if (itemViewModel != null)
            {
                if (itemViewModel.FeedModel.UpdateOn != feed.UpdateOn)
                {
                    viewModelCollection.Remove(itemViewModel);
                    var viewModel = this.Container.Resolve<ITodoItemViewModel>().SetModel(feed);
                    viewModelCollection.InsertItemAtFirst(viewModel);
                }
                if (itemViewModel.FeedModel.LastChangeOn != feed.LastChangeOn)
                {
                    itemViewModel.IsRead = feed.IsRead.Value;
                    itemViewModel.IsMark = feed.IsMark.Value;
                    if (!feed.IsAvailable.Value)
                    {
                        viewModelCollection.Remove(itemViewModel);
                    }
                }
            }
            else
            {
                if (feed.IsAvailable.Value)
                {
                    var viewModel = this.Container.Resolve<ITodoItemViewModel>().SetModel(feed);
                    viewModelCollection.InsertItemAtFirst(viewModel);
                }
            }
        }
    }
}
